#include "audio_enc_imp.h"
#include "hal_stream_convert.h"
#include "base_log.h"
#include <map>
#include <mutex>

namespace El {
namespace Hal {

IAudioEnc &IAudioEnc::GetInstance(int32_t channel, int32_t stream)
{
    static std::map<std::pair<int32_t, int32_t>, AudioEnc *> instances;
    static std::mutex mutex;

    std::lock_guard<std::mutex> lock(mutex);
    auto key = std::make_pair(channel, stream);
    auto it = instances.find(key);
    if (it == instances.end()) {
        AudioEnc *instance = new AudioEnc();
        instances.emplace(key, instance);
        return *instance;
    }
    return *it->second;
}

int32_t AudioEnc::Start(PacketCallback cb)
{
    if (!start_) {
        start_ = true;
    } else {
        return 0;
    }
    
    StreamConverter::GetInstance().RegisterAudioCallback([cb](const AudioEncPacket &frame) {
        cb(frame);
    });

    return 0;
}

void AudioEnc::Stop(int32_t handle)
{
    (void)handle;
    El::Hal::StreamConverter::GetInstance().Stop();

    StreamConverter::GetInstance().RegisterAudioCallback(nullptr);
}

bool AudioEnc::GetEncFormat(AudioEncFormat &enc)
{
    return El::Hal::StreamConverter::GetInstance().GetAudioInfo(enc);
}

}
}